package org.deadline.model.user;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.config.EnumerationDataOnDemand;
import org.deadline.repository.user.UserMailMessageRepository;
import org.deadline.service.user.UserMailMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Component
@Configurable
public class UserMailMessageDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<UserMailMessage> data;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    UserMailMessageService userMailMessageService;

	@Autowired
    UserMailMessageRepository userMailMessageRepository;

	public UserMailMessage getNewTransientUserMailMessage(int index) {
        UserMailMessage obj = new UserMailMessage();
        setBody(obj, index);
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setMessageType(obj, index);
        setModificationDate(obj, index);
        setRecipientCC(obj, index);
        setRecipientTo(obj, index);
        setSender(obj, index);
        setSubject(obj, index);
        return obj;
    }

	public void setBody(UserMailMessage obj, int index) {
        String body = "body_" + index;
        if (body.length() > 15) {
            body = body.substring(0, 15);
        }
        obj.setBody(body);
    }

	public void setCreationDate(UserMailMessage obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(UserMailMessage obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setMessageType(UserMailMessage obj, int index) {
        String messageType = "messageType_" + index;
        if (messageType.length() > 15) {
            messageType = messageType.substring(0, 15);
        }
        obj.setMessageType(messageType);
    }

	public void setModificationDate(UserMailMessage obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setRecipientCC(UserMailMessage obj, int index) {
        String recipientCC = "recipientCC_" + index;
        if (recipientCC.length() > 15) {
            recipientCC = recipientCC.substring(0, 15);
        }
        obj.setRecipientCC(recipientCC);
    }

	public void setRecipientTo(UserMailMessage obj, int index) {
        String recipientTo = "recipientTo_" + index;
        if (recipientTo.length() > 15) {
            recipientTo = recipientTo.substring(0, 15);
        }
        obj.setRecipientTo(recipientTo);
    }

	public void setSender(UserMailMessage obj, int index) {
        Person sender = personDataOnDemand.getRandomPerson();
        obj.setSender(sender);
    }

	public void setSubject(UserMailMessage obj, int index) {
        String subject = "subject_" + index;
        if (subject.length() > 15) {
            subject = subject.substring(0, 15);
        }
        obj.setSubject(subject);
    }

	public UserMailMessage getSpecificUserMailMessage(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        UserMailMessage obj = data.get(index);
        Long id = obj.getId();
        return userMailMessageService.findUserMailMessage(id);
    }

	public UserMailMessage getRandomUserMailMessage() {
        init();
        UserMailMessage obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return userMailMessageService.findUserMailMessage(id);
    }

	public boolean modifyUserMailMessage(UserMailMessage obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = userMailMessageService.findUserMailMessageEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'UserMailMessage' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<UserMailMessage>();
        for (int i = 0; i < 10; i++) {
            UserMailMessage obj = getNewTransientUserMailMessage(i);
            try {
                userMailMessageService.saveUserMailMessage(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            userMailMessageRepository.flush();
            data.add(obj);
        }
    }
}
